/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2020-12-18 16:49
**/
package config

import "github.com/spf13/viper"

// 数据库注册驱动
type DbproxyDbCommon struct {
	Dialect   string `mapstructure:"dialect"`
}

// api数据库配置
type DbproxyDbConf struct {
	Host               string
	User               string
	PassWd             string
	Db                 string
	Port               int64
	MaxOpenConnections int
	MaxIdleConnections int
	ConnMaxLifetime    int
}

// 主数据库配置信息
type DbproxyDbWdb struct {
	Host   string `mapstructure:"w_host"`
	User   string `mapstructure:"w_user"`
	PassWd string `mapstructure:"w_pass"`
	Db     string `mapstructure:"w_db"`
	Port   int64  `mapstructure:"w_port"`
}

// 从数据库配置信息
type DbproxyDbRdb struct {
	Host   string `mapstructure:"r_host"`
	User   string `mapstructure:"r_user"`
	PassWd string `mapstructure:"r_pass"`
	Db     string `mapstructure:"r_db"`
	Port   int64  `mapstructure:"r_port"`
}

// api日志配置
type DbproxyLogConfig struct {
	Level      string `mapstructure:"level"`
	Filename   string `mapstructure:"filename"`
	MaxSize    int    `mapstructure:"maxsize"`
	MaxAge     int    `mapstructure:"max_age"`
	MaxBackups int    `mapstructure:"max_backups"`
}


// 获取主从数据库相关配置
func GetDbproxyDbConf() (DatabaseList map[string][]DbproxyDbConf) {
	var wdbArr, rdbArr []DbproxyDbConf
	wdb := DbproxyDbConf{
		Host:               viper.GetString("dbproxy-db-wdb.w_host"),
		User:               viper.GetString("dbproxy-db-wdb.w_user"),
		PassWd:             viper.GetString("dbproxy-db-wdb.w_pass"),
		Db:                 viper.GetString("dbproxy-db-wdb.w_db"),
		Port:               viper.GetInt64("dbproxy-db-wdb.w_port"),
		MaxOpenConnections: viper.GetInt("dbproxy-db-wdb.w_maxOpenConnections"),
		MaxIdleConnections: viper.GetInt("dbproxy-db-wdb.w_maxIdleConnections"),
		ConnMaxLifetime:    viper.GetInt("dbproxy-db-wdb.w_connMaxLifetime"),
	}
	wdbArr = append(wdbArr, wdb)
	rdb := DbproxyDbConf{
		Host:               viper.GetString("dbproxy-db-rdb.r_host"),
		User:               viper.GetString("dbproxy-db-rdb.r_user"),
		PassWd:             viper.GetString("dbproxy-db-rdb.r_pass"),
		Db:                 viper.GetString("dbproxy-db-rdb.r_db"),
		Port:               viper.GetInt64("dbproxy-db-rdb.r_port"),
		MaxOpenConnections: viper.GetInt("dbproxy-db-rdb.r_maxOpenConnections"),
		MaxIdleConnections: viper.GetInt("dbproxy-db-rdb.r_maxIdleConnections"),
		ConnMaxLifetime:    viper.GetInt("dbproxy-db-rdb.r_connMaxLifetime"),
	}
	rdbArr = append(rdbArr, rdb)
	return map[string][]DbproxyDbConf{
		"w": wdbArr,
		"r": rdbArr,
	}

}
// 基础信息
type DbproxyBase struct {
	Port string `mapstructure:"port"`
}
// etcd配置信息
type DbproxyEtcd struct {
	Host string `mapstructure:"host"`
}
// 总的配置信息
type DbproxyConfig struct {
	DbproxyBase     DbproxyBase     `mapstructure:"dbproxy-base"`
	DbproxyEtcd     DbproxyEtcd     `mapstructure:"dbproxy-etcd"`
	DbproxyDbCommon DbproxyDbCommon `mapstructure:"dbproxy-db-common"`
	DbproxyDbWdb    DbproxyDbWdb    `mapstructure:"dbproxy-db-wdb"`
	DbproxyDbRdb    DbproxyDbRdb    `mapstructure:"dbproxy-db-rdb"`
	DbproxyZaplog   DbproxyLogConfig   `mapstructure:"dbproxy-zaplog"`
	DbproxyDbconfig map[string][]DbproxyDbConf
}

